﻿using System;
using System.IO;
using System.Net.Sockets;
using UnityEngine;

namespace Framework.Network
{

    /// <summary>
    /// 默认使用大二端，包头，而序列化的内容，有对应的action进行绑定
    /// </summary>
    public class DefaultNetworkChannelPacketHelper : INetworkChannelHelper
    {

        // public Action<byte[]> PacketCallBack;
        public void Init()
        {
            
        }

        public int DeserializePacketHeader(Stream destination)
        {
            byte[] bytes = new byte[2];
            destination.Position = 0;
            destination.Read(bytes, 0, 2);
            int length = (int)(bytes[0])*(1<<8) + bytes[1];
            return length;
        }

        public byte[] DeserializePacket(Stream destination)
        {
            byte[] bytes = new byte[destination.Length];
            destination.Position = 0;
            destination.Read(bytes, 0,
                (int)(destination.Length - destination.Position));
            // Log.Info($"收到包，{destination.Length},{System.Text.Encoding.UTF8.GetString(bytes)}");
            return bytes;
        }

 
        public long HeaderLength => sizeof(byte) * 2;







    }

}